import {
  activityOrders,
  dicEnum
} from '../../utils/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orders: [],
    orderDateIndex: 2,
    orderStatusIndex: 3,
    orderBuyIndex: 0,
    orderDateTypes: [],
    orderStatusTypes: [],
    orderBuyTypes: [],
    show: false,
    isOver: false,
    page: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    Promise.all([this.orderDate(),
      this.orderStatus(), this.orderBuy()
    ]).then(() => {
      this.activityOrders()
    })
  },

  /**
   * 查询团队订单
   */
  activityOrders() {
    activityOrders({
      dateType: this.data.orderDateTypes[this.data.orderDateIndex].code,
      status: this.data.orderStatusTypes[this.data.orderStatusIndex].code,
      page: this.data.page++,
      type: this.data.orderBuyTypes[this.data.orderBuyIndex].code,
      size: 10
    }).then(res => {
      if (res.data) {
        this.setData({
          orders: this.data.orders.concat(res.data)
        })
      } else {
        this.setData({
          isOver: true,
          show: false
        })
      }
    })
  },
  /**
   * 订单状态
   */
  orderStatus() {
    return new Promise((resolve, reject) => {
      dicEnum({
        type: 'activityOrderStatus'
      }).then(res => {
        this.setData({
          orderStatusTypes: res.data
        })
        resolve(true)
      })
    })
  },
  /**
   * 订单日期
   */
  orderDate() {
    return new Promise((resolve, reject) => {
      dicEnum({
        type: 'dateCondition'
      }).then(res => {
        this.setData({
          orderDateTypes: res.data
        })
        resolve(true)
      })
    })
  },
  /**
   * 下单类型
   */
  orderBuy() {
    return new Promise((resolve, reject) => {
      dicEnum({
        type: 'orderBuyType'
      }).then(res => {
        this.setData({
          orderBuyTypes: res.data
        })
        resolve(true)
      })
    })
  },

  /**
   * 选择订单日期
   */
  onOrderDate(e) {
    this.setData({
      orderDateIndex: e.currentTarget.dataset.index,
      orders: [],
      page: 1,
      isOver: false
    })
    this.activityOrders()
  },

  /**
   * 选择订单状态
   */
  onOrderStatus(e) {
    this.setData({
      orderStatusIndex: e.currentTarget.dataset.index,
      orders: [],
      page: 1,
      isOver: false
    })
    this.activityOrders()
  },
  /**
   * 选择订单下单类型
   */
  onOrderBuyType(e) {
    this.setData({
      orderBuyIndex: e.currentTarget.dataset.index,
      orders: [],
      page: 1,
      isOver: false
    })
    this.activityOrders()
  },
  // 上拉触底函数
  onReachBottom: function () {
    if (!this.data.isOver) {
      this.setData({
        show: true
      })
      setTimeout(() => {
        this.activityOrders()
      }, 1000);
    }
  },
})